import React, {Component} from 'react';
import AlibcaMgr from '@/sdkManager/AlibabaManager';
import {
  StyleSheet,
  SafeAreaView,
  Text,
  TouchableOpacity,
  NativeModules,
} from 'react-native';

const CalendarManager = NativeModules.CalendarManager;

export default class TestComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '测试',
      isLogin: '没登录',
      userInfo: '用户数据',

      Pid: 'mm_169220122_1290100019_110104050415', //三段式
      Adzoneid: '110104050415', //pid三段式最后一段
      taokeAppkey: '28617401', //阿里妈妈那边网站或者app的appkey

      OpenType: 'Auto', //打开的方式
      clientType: 'taobao', //taobao---唤起淘宝客户端；tmall---唤起天猫客户端
      BACK_URL: 'alisdk://', //唤端返回的scheme
      AlibcFailModeType: 'AlibcNativeFailModeJumpH5', //（唤端失败模式）
      isvcode: 'app', //必须传
    };
  }

  //处理参数
  mkParam = param => {
    param.Pid = param.Pid && param.Pid !== '' ? param.Pid : this.state.Pid;
    param.Adzoneid =
      param.Adzoneid && param.Adzoneid !== ''
        ? param.Adzoneid
        : this.state.Adzoneid;
    param.taokeAppkey =
      param.taokeAppkey && param.taokeAppkey !== ''
        ? param.taokeAppkey
        : this.state.taokeAppkey;

    param.OpenType =
      param.OpenType && param.OpenType !== ''
        ? param.OpenType
        : this.state.OpenType;
    param.clientType =
      param.clientType && param.clientType !== ''
        ? param.clientType
        : this.state.clientType;
    param.BACK_URL =
      param.BACK_URL && param.BACK_URL !== ''
        ? param.BACK_URL
        : this.state.BACK_URL;
    param.AlibcFailModeType =
      param.AlibcFailModeType && param.AlibcFailModeType !== ''
        ? param.AlibcFailModeType
        : this.state.AlibcFailModeType;
    param.isvcode =
      param.isvcode && param.isvcode !== ''
        ? param.isvcode
        : this.state.isvcode;

    param.BizCodeStr =
      param.BizCodeStr && param.BizCodeStr !== '' ? param.BizCodeStr : '';
    param.sellerId =
      param.sellerId && param.sellerId !== '' ? param.sellerId : '';
    return param;
  };

  render() {
    return (
      <SafeAreaView>
        <Text>===================================测试===================</Text>
        <Text style={style.container}>{this.state.text}</Text>
        <TouchableOpacity
          onPress={() => {
            CalendarManager.addEvent(
              'Birthday Party',
              '4 Privet Drive, Surrey',
            );
            // AlibcaMgr.show('点击了', 2, msg => {
            //   this.setState({
            //     text: msg,
            //   });
            // });
          }}>
          <Text style={style.container}>测试</Text>
        </TouchableOpacity>
        <Text>
          ===================================初始化===================
        </Text>
        <TouchableOpacity
          onPress={() => {
            AlibcaMgr.asyncInit((code, map) => {
              this.setState({
                text: map.code,
              });
            });
          }}>
          <Text style={style.container}>初始化 asyncInit</Text>
        </TouchableOpacity>
        <Text>===================================登录===================</Text>
        <TouchableOpacity
          onPress={() => {
            AlibcaMgr.showLogin((code, param) => {
              this.setState({
                text: code,
                isLogin: code == 0 ? '已经登录' : '没登录',
              });
            });
          }}>
          <Text style={style.container}>{this.state.isLogin}</Text>
        </TouchableOpacity>
        <Text>
          ===================================是否已经登录===================
        </Text>
        <TouchableOpacity
          onPress={() => {
            AlibcaMgr.isLogin((code, param) => {
              this.setState({
                text: code,
                isLogin: code == 0 ? '已经登录' : '没登录',
              });
            });
          }}>
          <Text style={style.container}>{this.state.isLogin}</Text>
        </TouchableOpacity>
        <Text>
          ===================================获取用户信息===================
        </Text>
        <TouchableOpacity
          onPress={() => {
            AlibcaMgr.getUserInfo((code, param) => {
              this.setState({
                text: code,
                userInfo:
                  code == 0
                    ? param.nick + ',' + param.avatarUrl + ',' + param.openId
                    : '登录失败!',
              });
            });
          }}>
          <Text style={style.container}>{this.state.userInfo}</Text>
        </TouchableOpacity>
        <Text>
          ===================================退出登录===================
        </Text>
        <TouchableOpacity
          onPress={() => {
            AlibcaMgr.logout((code, param) => {
              this.setState({
                text: code,
                isLogin: code == 0 ? '已退出登录' : '已经登录',
              });
            });
          }}>
          <Text style={style.container}>退出登录</Text>
        </TouchableOpacity>
        <Text>===============================show=======================</Text>
        <TouchableOpacity
          onPress={() => {
            //详情页
            var parm = {
              //详情  （和 url 一样）
              //  itemid: "612996128437",
              //  BizCodeStr:"detail",  //detal  shop

              // 商店
              // shopid:"123367260",
              // BizCodeStr:"shop",  //detal  shop
              // sellerId:"123367260",      //使用shop方式就需要填写这个sellerId

              //购物车
              //BizCodeStr:"cart",  //detal  shop  cart(购物车)

              // url : "https://s.click.taobao.com/gkuRPnv"   //劵地址
              //  url:"https://s.click.taobao.com/NksRPnv"     //购买地址一样

              url: 'https://s.click.taobao.com/gvUBPnv',
            };
            var params = this.mkParam(parm);
            // AlibcaMgr.addEvent(
            //     {
            //         type: 'url', //detail shop mycard url
            //         payload: params,
            //       }
            //     , "2222");
            AlibcaMgr.showPage(
              {
                type: 'url', //detail shop mycard url
                payload: params,
              },
              (code, info) => {
                this.setState({
                  text: code,
                });
              },
            );
          }}>
          <Text style={style.container}>显示</Text>
        </TouchableOpacity>
      </SafeAreaView>
    );
  }
}

const style = StyleSheet.create({
  container: {
    color: 'blue',
    fontSize: 30,
  },
});
